﻿@model Simple_PM.Models.DashboardModel


<div id="mainCategory">
    
</div>

<script type="text/javascript">

    $(document).ready(function () {

        /* Load Categories
        ***************************************************************************************************************************/
        loadCategories();

        function loadCategories() {
            
            $("#mainCategory").text("");
            $("#mainCategory").append("<div class='loading'><img src='@Url.Content("~/Content/Loading/loading2.gif")' /></div>");

            var url = '@Url.Action("LoadCategories", "Task")';
            $.ajax({
                url: url,
                type: 'POST',
                cache: false,
                success: function (result) {
                    $("#mainCategory").hide();
                    $("#mainCategory").text("");
                    $("#mainCategory").append("<div id='newFeature' class='tags'>" + " <img src='@Url.Content("~/Content/Images/Category/new_feature.png")' /> <a href='#' >New Feature X " + result.NewFeatureCount + "</a></div>");
                    $("#mainCategory").append("<div id='change' class='tags'>" + " <img src='@Url.Content("~/Content/Images/Category/change.png")' /> <a href='#' >Change Request X " + result.ChangeRequestCount + "</a></div>");
                    $("#mainCategory").append("<div id='bug' class='tags'>" + " <img src='@Url.Content("~/Content/Images/Category/bug.png")' /> <a href='#' >Bugs X " + result.BugsCount + "</a></div>");
                    $("#mainCategory").append("<div id='todo' class='tags'>" + " <img src='@Url.Content("~/Content/Images/Category/todo.png")' /><a href='#' >Todo X " + result.TodoCount + "</a></div>");
                    $("#mainCategory").append("<div id='misc' class='tags'>" + " <img src='@Url.Content("~/Content/Images/Category/misc.png")' /><a href='#' >Miscellaneous X " + result.MiscCount + "</a></div>");
                    $("#mainCategory").fadeIn(400);

                },
                error: function () {
                    $("#mainCategory").text("");
                    $("#mainCategory").append("Error occured !");
                }
            });
        }

        /* Events
        ***************************************************************************************************************************/

        $('#newFeature').live('click', function () {
            showTask("NF", "");
        });

        $('#change').live('click', function () {
            showTask("CR", "");
        });

        $('#bug').live('click', function () {
            showTask("BUG", "");
        });

        $('#todo').live('click', function () {
            showTask("TODO", "");
        });

        $('#misc').live('click', function () {
            showTask("MISC", "");
        });

        $('#allOpenTasks').live('click', function () {
            showTask("", "All");
        });

        $('#allTasks').live('click', function () {
            showTask("", "Mix");
        });

        $('#completedTasks').live('click', function () {
            showTask("", "Completed");
        });

        /* Show Tasks - Function
        ***************************************************************************************************************************/

        function showTask(categoryId, filterStatus) {
            $("#mainTasks").text("");
            $("#mainTasks").append("<div class='loading'><img src='../../Content/Loading/loading2.gif' /></div>");

            //var url2 = '/Task/LoadTasks/?category=' + categoryId + '&filterStatus=' + filterStatus;
            var url2 = "@Url.Action("LoadTasks","Task")";
            $.ajax({
                url: url2,
                type: 'GET',
                cache: false,
                data:{category: categoryId, filterStatus:filterStatus},
                success: function (result) {
                    $("#mainCategory").hide();
                    $("#mainTasks").text("");
                    for (var i in result) {
                        var taskTitle = result[i].TaskTitle;
                        var taskId = result[i].TaskId;
                        var closedDate = result[i].TaskCompleted;
                        var status = result[i].TaskStatus;
                        
                        var dateScheduledJson = result[i].TaskScheduled;
                    
                        var dateScheduled;
                        if (dateScheduledJson == null) {
                            dateScheduled = "Scheduled for : No Date";
                        }
                        else {
                            var milli = dateScheduledJson.replace(/\/Date\((-?\d+)\)\//, '$1');
                            var d = new Date(parseInt(milli));
                            dateScheduled = "Scheduled for : " + d.toDateString();
                        }   

                        if (status == "1") {
                            $("#mainTasks").append("<div class='tasks' >" + taskTitle + "<br/> <div style='float:right'> <div class='reopenBtn' data-taskid='" + taskId + "'>Re-Open</div> <div class='dateBtn' data-taskid='" + taskId + "'>Closed on - " + closedDate + "</div> </div> </div>");
                        }
                        else {
                            $("#mainTasks").append("<div class='tasks' >" + taskTitle + "<br/> <div style='float:right'> <div class='doneBtn' data-taskid='" + taskId + "'>done</div> <div class='editBtn' data-taskid='" + taskId + "'>edit</div> <div class='deleteBtn' data-taskid='" + taskId + "'>delete </div> <div class='dateBtn'>" + dateScheduled + "</div>  </div> </div>");
                        }
                    }

                    if (result.length == 0) {
                        $("#mainTasks").text("Empty !");
                    }

                    $("#mainCategory").fadeIn(400);
                },
                error: function () {
                    $("#mainTasks").text("");
                    $("#mainTasks").append("Error occured !");
                }
            });
        }

    });

</script>